# vim:ft=tmux
# tmux display things in 256 colors
set -g default-terminal "tmux-256color"
set -as terminal-overrides ',xterm*:Tc:sitm=\E[3m'

set -g history-limit 20000

# automatically renumber tmux windows
set -g renumber-windows on

######################
#### General Settings ####
######################

 # set the prefix to control-a. This is more convenient than control-b
 # and 'a' is right next to Caps Lock (which I remap to control)
set -g prefix C-a
bind C-a send-prefix
bind-key a send-prefix

set -g set-clipboard on

# enable mouse support for switching panes/windows
setw -g mouse on

# make delay shorter
set -g escape-time 0
# make window/pane index start with 1
set -g base-index 1
setw -g pane-base-index 1
set-option -g focus-events on
set-option -g set-titles on

# set vi mode for copy mode
setw -g mode-keys vi
set -g status-keys vi

# automatically rename windows when an explicit name is not set
set-window-option -g automatic-rename on
set-option -g automatic-rename-format "#(nerdwin #{pane_current_command})"

# set the default title format
set-option -g set-titles-string "#T - #W"

# disabled activity monitoring
setw -g monitor-activity off
set -g visual-activity off

# Rather than constraining window size to the maximum size of any client
# connected to the *session*, constrain window size to the maximum size of any
# client connected to *that window*. Much more reasonable.
setw -g aggressive-resize on

######################
#### Key Bindings ####
######################

# keep CWD when opening new window
unbind c
bind c new-window -c "#{pane_current_path}"

# tile all windows
unbind =
bind = select-layout tiled

# reload config file
bind r source-file ~/.config/tmux/tmux.conf \; display "Config Reloaded!"

# split window and fix path for tmux 1.9
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"

# synchronize all panes in a window
bind y setw synchronize-panes

# pane movement shortcuts
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

bind -r C-h select-window -t :-
bind -r C-l select-window -t :+

# Resize pane shortcuts
bind -r H resize-pane -L 10
bind -r J resize-pane -D 10
bind -r K resize-pane -U 10
bind -r L resize-pane -R 10

# open lazygit in a new window
bind-key g display-popup -w "80%" -h "80%" -d "#{pane_current_path}" -E "lazygit"

# toggle the status bar
bind-key -T prefix T set-option -g status

unbind s
bind s display-popup -E "\
  tmux list-sessions -F '#{?session_attached,,#{session_name}} ' |\
  sed '/^\s*$/d' |\
  fzf --reverse --header jump-to-session |\
  xargs tmux switch-client -t"

# more settings to make copy-mode more vim-like
unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind -T copy-mode-vi v send -X begin-selection

if-shell '[ "$(defaults read -g AppleInterfaceStyle 2>/dev/null)" = "Dark" ]' \
  'source-file "$DOTFILES/config/tmux/themes/catppuccin/mocha.conf"' \
  'source-file "$DOTFILES/config/tmux/themes/catppuccin/latte.conf"'
source-file "$DOTFILES/config/tmux/themes/catppuccin.conf"

if-shell "[ -z \"$TMUX_MINIMAL\" ]" {
  set -g status on
} {
  set -g status off
  set-hook -g after-new-window      'if "[ #{session_windows} -gt 1 ]" "set status on"'
  set-hook -g after-kill-pane       'if "[ #{session_windows} -lt 2 ]" "set status off"'
  set-hook -g pane-exited           'if "[ #{session_windows} -lt 2 ]" "set status off"'
  set-hook -g window-layout-changed 'if "[ #{session_windows} -lt 2 ]" "set status off"'
}
